import {
    reqLogin,reqUserInfo
} from '@/api'

const state = {
    userInfo: {},
    isShowLogin:false
}
const mutations = {
    USERINFO(state, userInfo) {
        state.userInfo = userInfo
    },
    ISSHOWLOGIN(context,flag){
        context.isShowLogin = flag
    }
}
const actions = {
    async login({
        commit
    }, loginInfo) {
        let userInfo = await reqLogin(loginInfo)
        if (userInfo.code == 200) {
            // 将cookie和用户信息保存到本地存储
            if (typeof Storage !== "undefined") {
                localStorage.setItem("cookie", userInfo.cookie);
                localStorage.setItem("uid", userInfo.account.id);
            } else {
                // 抱歉！不支持 Web Storage ..
                alert("不支持本地存储");
            }
            commit("ISSHOWLOGIN",false)
            commit("USERINFO", userInfo.profile)
        }
    },

    async getUserInfo({commit},uid){
        let result = await reqUserInfo(uid)
        if(result.code == 200){
            commit("USERINFO", result.profile)
        }
    },

    showLogin({commit},flag){
        commit("ISSHOWLOGIN", flag)
    }
}

export default {
    state,
    mutations,
    actions
}